package top.yxqz.session.demo.web;

import top.yxqz.session.demo.school.Student;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Mr.余
 * @date 2022/11/25
 * @package top.yxqz.session.demo.web
 * 学生登录
 */
@WebServlet("/stu/login")
public class StudentLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//       创建相关对象
        response.setContentType("text/html;charset=utf-8");
        ServletContext context = getServletContext();
        ServletConfig config = getServletConfig();
        PrintWriter writer = response.getWriter();

        writer.println("<h1>Session自动登录学习</h1>");
//       创造数据
        String usernamedata = "zhangfei";
        String passwordData = "123456";


//        得到应用上下文
        String contextPath = context.getContextPath();
//        得到当前servlet的名字
        String servletName = config.getServletName();
        String uri = request.getRequestURI();


        writer.println("<form action=" + uri + ">\n" +
                "    用户名：<input type=\"text\" name=\"userName\"> <br>\n" +
                "    密 码：<input type=\"password\" name=\"password\"> <br>\n" +
                "    七天无理由登录 <input type=\"checkbox\" name=\"checkbox\" value=\"checkbox\"> <br>\n" +
                "    <input type=\"submit\" value=\"提交\">\n" +
                "</form>");

        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String checkbox = request.getParameter("checkbox");
        System.out.println("前端输入的信息：" + userName);
        System.out.println("前端输入的信息：" + password);
        System.out.println("前端输入的信息：" + checkbox);


        HttpSession old_session = request.getSession();
        Student user = (Student) old_session.getAttribute("user");
        if (user != null) {
            response.getWriter().write("您已登录，欢迎您" + user.getUserName());
            response.getWriter().write("<a href=" + getServletContext().getContextPath() + "/sessionLogoutServlet>退出</a>");
            return;
        }


//        使用对象来装数据
        Student student = new Student(userName, password);
        //        手动登录处理
        if (usernamedata.equals(userName) && passwordData.equals(password)) {
            //        判定用户是否是不选中了复选框
            if ("checkbox".equals(checkbox)) {
                System.out.println("用户勾选了自动登录的复选框");
//                设置Session以及修改cookie
                HttpSession session = request.getSession();
                session.setAttribute("student", student); //将学生信息，存储到Session域中
                Cookie cookie = new Cookie("JSESSIONID", session.getId());//修改JSESSIONID这个cookie的信息
                cookie.setPath("/stu");
                cookie.setMaxAge(60 * 60 * 24 * 7);
                response.addCookie(cookie);
            }
//                请求转发 到登录成功的页面
//            request.getRequestDispatcher("/WEB-INF/success.html").forward(request, response);
            response.getWriter().write("您已登录，欢迎您" + userName);
            response.getWriter().write("<a href=" + getServletContext().getContextPath() + "/sessionLogoutServlet>退出</a>");

        }
    }
}
